package com.wanpinghui.supplier.business;

import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.widgets.CustomVideoView;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.cameralibrary.util.FileUtil;
import com.yunshl.yunshllibrary.utils.TextUtil;

import org.xutils.view.annotation.ContentView;

/**
 * Created by tg596 on 2018/1/26.
 */
@ContentView(R.layout.layout_activity_video_preview)
public class VideoPreviewActivity extends AppCompatActivity {


    private static final int REQUEST_DELETE_VIDEO = 0x16; // 删除视频

    private String videopath;
 //   private TitlePanelLayoutUser mTplTitle;
    private CustomVideoView mVideoView;
 //   private RelativeLayout mLlRootview;


    public void bindEvents() {
//        tpl_title.setOnClickBacktrack(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
//        tpl_title.setOnClickAddition(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                FileUtil.deleteFile(videopath);
//                setResult(REQUEST_DELETE_VIDEO);
//                finish();
//            }
//        });
//        mLlRootview.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                    finishAfterTransition();
//                } else {
//                    finish();
//                }
//            }
//        });
        mVideoView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    finishAfterTransition();
                } else {
                    finish();
                }
//                getWindow().setFlags(
//                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
//                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

                return false;
            }
        });

    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_activity_video_preview);
        initView();
        bindEvents();
    }

    public void initView() {
    //    mTplTitle = (TitlePanelLayoutUser) findViewById(R.id.tpl_title);
        mVideoView = (CustomVideoView) findViewById(R.id.video_view);
    //    mLlRootview = (RelativeLayout) findViewById(R.id.ll_rootview);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
       // mTplTitle.setAddViewRes(ContextCompat.getDrawable(this, R.drawable.address_list_delete_picture));
        videopath = getIntent().getStringExtra("path");
        if (TextUtil.isNotEmpty(videopath)) {
            mVideoView.setVideoPath(videopath);

            mVideoView.start();
//            mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//
//                @Override
//                public void onPrepared(MediaPlayer mp) {
//                    mp.start();
//                    mp.setLooping(true);
//
//                }
//            });

            mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mVideoView.setVideoPath(videopath);
                    mVideoView.start();

                }
            });
        }


    }
}
